#include "stdafx.h"
#include "GUI.h"
#include "..\..\Global\globaldef.h"

GUI::Button GUI::LightingMenu::g_SchemeButtons[GUI::LightingMenu::SCHEME_BUTTON_COUNT] = {GUI::Button()};
GUI::Button GUI::LightingMenu::g_ControlToggleButtons[GUI::LightingMenu::CONTROL_CHANNEL_COUNT] = {GUI::Button()};

RECT GUI::LightingMenu::g_menuRectangle = {0};
Render::tResource* GUI::LightingMenu::g_pBackground = NULL;
uint GUI::LightingMenu::g_state = 0;
D3DXVECTOR2 GUI::LightingMenu::Position = D3DXVECTOR2(0,-LIGHT_MENU_HEIGHT);

void GUI::LightingMenu::OnClickScheme(uint schemeID) {

}

void GUI::LightingMenu::OnLightParameter(uint parameterID, bool active, float dataA, float dataB) {

}

void GUI::LightingMenu::Draw() {
	
	if(GUI::LightingMenu::g_state != 0) {

		int dY = 0;
		switch(GUI::LightingMenu::g_state) {

			case 2://opening
				dY = 10;
				if(LightingMenu::Position.y + dY >= 22) {

					dY = 22 - LightingMenu::Position.y;
					GUI::LightingMenu::g_state = 1;
				}
				break;
			case 3://closing
				dY = -10;
				if(LightingMenu::Position.y + dY <= -LIGHT_MENU_HEIGHT) {

					dY = -LIGHT_MENU_HEIGHT - LightingMenu::Position.y;
					GUI::LightingMenu::g_state = 0;
				}
				break;
		}
		
		LightingMenu::Position.y += dY;
		Render::Singleton()->DrawSprite(GUI::LightingMenu::g_pBackground,  LightingMenu::Position * 2, 
			D3DXVECTOR2(384 / GUI::Width(&g_menuRectangle), LIGHT_MENU_HEIGHT / GUI::Height(&g_menuRectangle)),
			&LightingMenu::g_menuRectangle);

		for(UINT i = 0; i < SCHEME_BUTTON_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_SchemeButtons[i];
			pButton->Position.y += dY;
			uint buttonWidth = 0;
			uint buttonHeight = 0;
			Render::Singleton()->GetTextureInfo(pButton->pImage, &buttonWidth, &buttonHeight);

			RECT br = pButton->Rectangle;
			switch(pButton->State) {
				default:
				case 0:
					br.top = 0;
					br.bottom = (buttonHeight / 3.0f);
					break;
				case 1:
					br.top = (buttonHeight / 3.0f);
					br.bottom = (buttonHeight / 3.0f) * 2.0f;
					break;
				case 2:
					br.top = (buttonHeight / 3.0f) * 2.0f;
					br.bottom = buttonHeight;
					break;
			};

			Render::Singleton()->DrawSprite(pButton->pImage, pButton->Position * 2, D3DXVECTOR2(1,1), &br);
		}

		for(UINT i = 0; i < CONTROL_CHANNEL_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_ControlToggleButtons[i];
			pButton->Position.y += dY;
			uint buttonWidth = 0;
			uint buttonHeight = 0;
			Render::Singleton()->GetTextureInfo(pButton->pImage, &buttonWidth, &buttonHeight);

			RECT br = pButton->Rectangle;
			switch(pButton->State) {
				default:
				case 0:
					br.top = 0;
					br.bottom = (buttonHeight / 3.0f);
					break;
				case 1:
					br.top = (buttonHeight / 3.0f);
					br.bottom = (buttonHeight / 3.0f) * 2.0f;
					break;
				case 2:
					br.top = (buttonHeight / 3.0f) * 2.0f;
					br.bottom = buttonHeight;
					break;
			};

			Render::Singleton()->DrawSprite(pButton->pImage, pButton->Position * 2, D3DXVECTOR2(1,1), &br);
		}
	}
}

void GUI::LightingMenu::Toggle() {

	if(g_state == 0 || g_state == 3) //closed
		g_state = 2; //opening
	else if(g_state == 1 || g_state == 2)  //open
		g_state = 3; //closing
}

void GUI::LightingMenu::OnMouseEvent(const int x, const int y, const int leftButton, const int rightButton) {
	
	if(GUI::LightingMenu::g_state != 0) {

		for(UINT i = 0; i < SCHEME_BUTTON_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_SchemeButtons[i];
			pButton->Update(leftButton);

			if(pButton->State == 2)
				OnClickScheme(i);
		}

		for(UINT i = 0; i < CONTROL_CHANNEL_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_ControlToggleButtons[i];
			pButton->Update(leftButton);
			
			if(pButton->State == 2)
				OnLightParameter(i, false, 0, 0);
		}
	}
}

bool GUI::LightingMenu::Create() {

	Render::tResource* pMenuBackground = NULL;
	Render::tResource* pControlButtons = NULL;

	try {

		if(!Render::Singleton()->CreateTexture("GUI\\menu_background.jpg", &pMenuBackground))
			throw tException("Failed to create background texture");

		g_menuRectangle.left = 0;
		g_menuRectangle.top = 0;
		g_menuRectangle.bottom = 4;
		g_menuRectangle.right = 4;

		GUI::LightingMenu::Position.y = -GUI::LightingMenu::LIGHT_MENU_HEIGHT;
		GUI::LightingMenu::Position.x = 0;

		uint firstOffset = 10;
		uint schemeRowHeight = 0;

		for(UINT i = 0; i < SCHEME_BUTTON_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_SchemeButtons[i];

			if(!Render::Singleton()->CreateTexture("GUI\\lighting_sun.jpg", &pButton->pImage))
				throw tException("Failed to create button texture");
					
			uint buttonWidth = 0;
			uint buttonHeight = 0;
			Render::Singleton()->GetTextureInfo(pButton->pImage, &buttonWidth, &buttonHeight);
			
			RECT* pRect = &pButton->Rectangle;
			pRect->left = 0;
			pRect->top = 0;
			pRect->right = buttonWidth;
			pRect->bottom = buttonHeight / 3;

			pButton->Position = D3DXVECTOR2(buttonWidth * i, firstOffset) + GUI::LightingMenu::Position; 

			schemeRowHeight = firstOffset + (buttonHeight / 3);
		};

		if(!Render::Singleton()->CreateTexture("GUI\\light_control_toggle.jpg", &pControlButtons))
			throw tException("Failed to create buttons texture");
		uint buttonWidth = 0;
		uint buttonHeight = 0;
		Render::Singleton()->GetTextureInfo(pControlButtons, &buttonWidth, &buttonHeight);
		buttonWidth /= 3;
		buttonHeight /= 3;

		for(UINT i = 0; i < CONTROL_CHANNEL_COUNT; i++) {

			Button* pButton = &GUI::LightingMenu::g_ControlToggleButtons[i];
			pButton->pImage = pControlButtons;
			
			RECT* pRect = &pButton->Rectangle;
			pRect->left = i * buttonWidth;
			pRect->right = (i + 1) * buttonWidth;
			pRect->top = 0;
			pRect->bottom = buttonHeight;

			pButton->Position = D3DXVECTOR2(0, firstOffset+schemeRowHeight + (buttonHeight * i)) + GUI::LightingMenu::Position; 
		};

		GUI::LightingMenu::g_pBackground = pMenuBackground;
		return true;

	}catch(tException e) {

		//Render::Singleton()->ReleaseResource(&pMenuBackground);

		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::LightingMenu::Release() {

	Render::Singleton()->ReleaseResource(&GUI::LightingMenu::g_pBackground);
	return true;
}